﻿using CN.T.Backend.Application.System.Dtos;
using CN.T.Backend.Application.System.Services;
using CN.T.Backend.Application.System.Utility;
using DRNetCore.Api.Models;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace CN.T.Backend.Application.System
{
    /// <summary>
    /// 凭证管理
    /// </summary>
    public class VoucherAppService : IDynamicApiController, IVoucherService, ITransient
    {
        private readonly IMemoryCache _memoryCachedal;
        public VoucherAppService(IMemoryCache memoryCachedal)
        {
            _memoryCachedal = memoryCachedal;
        }
        /// <summary>
        /// 凭证查询
        /// </summary>
        /// <param name="dtoVoucherQuery"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<object> PostVoucher(DtoVoucherQuery dtoVoucherQuery,string token)
        {
            var header1 = new List<KeyValuePair<string, string>>();
            header1.Add(new KeyValuePair<string, string>("appKey", App.Configuration["T+:appKey"]));
            header1.Add(new KeyValuePair<string, string>("appSecret", App.Configuration["T+:appSecret"]));
            header1.Add(new KeyValuePair<string, string>("Content-Type", "application/json"));
            header1.Add(new KeyValuePair<string, string>("openToken", token));
            var jsondto=JsonConvert.SerializeObject(dtoVoucherQuery);
            var lssqmurl = $"https://openapi.chanjet.com/tplus/api/v2/doc/Query";
            var Postdata =await NetHttpHelp.PostJsonToEntityAsync<object>(lssqmurl, jsondto, header1);
            return Postdata.ToString();
        }
        /// <summary>
        /// 凭证新增
        /// </summary>
        /// <param name="dtoCreateVoucher"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<object> PostCreateVoucher(DtoCreateVoucher dtoCreateVoucher, string token)
        {
            var header1 = new List<KeyValuePair<string, string>>();
            header1.Add(new KeyValuePair<string, string>("appKey", App.Configuration["T+:appKey"]));
            header1.Add(new KeyValuePair<string, string>("appSecret", App.Configuration["T+:appSecret"]));
            header1.Add(new KeyValuePair<string, string>("Content-Type", "application/json"));
            header1.Add(new KeyValuePair<string, string>("openToken", token));
            var jsondto = JsonConvert.SerializeObject(dtoCreateVoucher);
            var lssqmurl = $"https://openapi.chanjet.com/tplus/api/v2/doc/Query";
            var Postdata = await NetHttpHelp.PostJsonToEntityAsync<object>(lssqmurl, jsondto, header1);
            return Postdata.ToString();
        }
    }
}
